package com.heima.freemarker.test;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yuchuang
 * @Date: 2024-01-18 22:56
 * @Description:
 */
public class CGLIBtest {

    @Test
    public  void test02() {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CDLIB.class);
        enhancer.setCallback(new CGLIBIntercepter());
        CDLIB o = (CDLIB)enhancer.create();
        o.test();

    }

}


    class CDLIB {
        public CDLIB() {
            System.out.println("目标对象的构造方法!!");
        }
        public void test() {
            System.out.println("要代理的对象方法");
        }
    }

    class CGLIBIntercepter implements MethodInterceptor  {
        /**
         * sub：cglib生成的代理对象
         * method：被代理对象方法
         * objects：方法入参
         * methodProxy: 代理方法
         */
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            System.out.println("增强之前的逻辑");
            Object o1 = methodProxy.invokeSuper(o, objects);
            System.out.println("增强之后的逻辑");
            return o1;
        }
    }



